<?php

namespace App\Console\Commands;

use App\Model\Device;
use App\Model\DeviceTwo;
use App\Model\InverterData;
use App\Model\InverterDataThree;
use App\Model\InverterDataTwo;
use App\Model\Site;
use App\Model\StatisticsDayEnergy;
use App\Model\StatisticsDeviceDayEnergy;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Statistic extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'statistic {target}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $target = $this->argument('target');
        if ($target == 'site_day_energy') {
            $this->statisticSiteDayEnergy();
        }
        if ($target == 'device_day_energy') {
            $this->statisticDeviceDayEnergy();
        }
    }

    private function statisticSiteDayEnergy()
    {
        //每天22点运行
        $date = date('Y-m-d');//获取当天
//        $date = "2020-10-23";
        DB::beginTransaction();
        $sites = Site::get();
        foreach ($sites as $site) {
            $sde = StatisticsDayEnergy::getOneDayBySiteId($site->id, $date);
            if (!$sde) {
                $sde = new StatisticsDayEnergy();
                $sde->site_id = $site->id;
                $sde->user_id = $site->user_id;
                $sde->date = $date;
                $site_id = $site->id;
                switch ($site_id) {
                    case $site_id < 18:
                        $site_value = InverterData::getOneDayGenCapBySiteId($site->id, $date);
                        $sde->site_value = $site_value;
                        $sde->user_value = $site_value;
                        break;
                    case $site_id >= 18 && $site_id < 43:
                        $site_value = InverterDataTwo::getOneDayGenCapBySiteId($site->id, $date);
                        $sde->site_value = $site_value;
                        $sde->user_value = $site_value;
                        break;
                    default:
                        $site_value = InverterDataThree::getOneDayGenCapBySiteId($site->id, $date);
                        $sde->site_value = $site_value;
                        $sde->user_value = $site_value;
                }
                $s = $sde->save();
                if(!$s){
                    DB::rollback();
                }
            }else{
                $sdes = StatisticsDayEnergy::getOneDayByUserId($site->user_id, $date);
                $user_value = 0;
                foreach ($sdes as $s) {
                    $user_value = $user_value + $s->site_value;
                }
                foreach ($sdes as $s) {
                    $s->user_value = $user_value;
                    $r = $s->save();
                    if(!$r){
                        DB::rollback();
                    }
                }
            }
            DB::commit();

        }

        $this->info('Completed site day energy');

    }

    private function statisticDeviceDayEnergy()
    {
        //每天22点运行
        $date = date('Y-m-d');//获取当天
//        $date = "2020-10-23";
//        $date = '2019-03-18';
        DB::beginTransaction();
        $devices = Device::orderBy('site_id')->get();
        foreach ($devices as $device) {
            $sdde = StatisticsDeviceDayEnergy::getOneDayByDeviceId($device->user_id, $device->device_id, $date);
            if (!$sdde) {
                $param['device_id'] = $device->device_id;
                $param['user_id'] = $device->user_id;
                $param['site_id'] = $device->site_id;
                $param['date'] = $date;
                $site_id = $device->site_id;

                if ($site_id < 18) {
                    $param['value'] = InverterData::getOneDayGenCapByDeviceId($device->user_id, $device->device_id, $date);
                }
                if ($site_id >= 18 && $site_id < 43) {
                    $param['value'] = InverterDataTwo::getOneDayGenCapByDeviceId($device->user_id, $device->device_id, $date);
                }
                if ($site_id >= 43) {
                    $param['value'] = InverterDataThree::getOneDayGenCapByDeviceId($device->user_id, $device->device_id, $date);
                }
                $obj = StatisticsDeviceDayEnergy::create($param);
                if(!$obj){
                    DB::rollback();
                }
            }
        }
        DB::commit();
        $this->info('Completed device day energy');
    }
}
